'''
Created on Dec 21, 2009

@author: Roni
'''
import orchid
import graph
import logging
import cliques.utils
import urllib
import cliques.cliqueFinder
from xml.sax import saxutils
import re
import webgraphs.intraSiteAnalyzer

class googleScholarAnalyzer(webgraphs.intraSiteAnalyzer.intraSiteAnalyzer):
    '''
    classdocs
    '''
        
    def __init__(self, linksToFetchAndCond, siteQueueAndCond, db,out_file_name , file_format):
        '''
        Constructor
        '''
        webgraphs.intraSiteAnalyzer.intraSiteAnalyzer.__init__(self,linksToFetchAndCond,\
                                                               siteQueueAndCond, db,\
                                                               out_file_name , file_format)
        
        self.search_seeds = list(linksToFetchAndCond[0].values()[0])
        self.title_pattern = re.compile('document.title="(?P<itemname>.*)"')
        self.filter_pattern = re.compile("^http://scholar.google.com/scholar\?cites=.*$")
        self.hostname_to_title = dict()
        self.urlHandler = orchid.UrlHandler()
        # document.title="(?P<itemname>.*)
        
    def analyzeSite(self, db, site):
        ''' Parse cite name from content '''    
        webgraphs.intraSiteAnalyzer.intraSiteAnalyzer.analyzeSite(self, db, site)

        # Parse the site and find the name of the title of the paper
        if site.stringUrl in self.search_seeds:
            self.hostname_to_title[site.stringUrl] = site.stringUrl
        else:    
            self.hostname_to_title[site.stringUrl] = self.extract_title(site.rawContent)

    def extract_title(self,site_content):
        ''' Extract the title of the paper from a google scholar citation page '''
        match = self.title_pattern.search(site_content)
        return match.group('itemname')
          
    def filter_site(self,str): 
        if str in self.search_seeds: # Keep the first page unprunned to allow analyzing it
            return str
        else:       
            result = self.filter_pattern.match(str)
            if result is not None:
                return result.group()
            else:
                return None

    def extract_page_name(self,str): 
        ''' Extract the item name from the hostname '''
        if self.hostname_to_title.has_key(str)==False:
            # Need to fetch the web page and parse it
            self.urlHandler.processUrl(str)
            site = self.urlHandler.getSite()
            rawContent = site.read()
            self.hostname_to_title[str]=self.extract_title(rawContent)
        return self.hostname_to_title[str] 
             
             